package com.goods.business.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.converter.ProductCategoryConverter;
import com.goods.business.converter.ProductCategoryTreeNodeConverter;
import com.goods.business.mapper.ProductCategoryMapper;
import com.goods.business.service.SuppliesCategoryService;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * 物资类别
 */
@Service
public class SuppliesCategoryServiceImpl implements SuppliesCategoryService {
    @Autowired
    private ProductCategoryMapper productCategoryMapper;
    @Autowired
    private ProductCategoryTreeNodeConverter productCategoryTreeNodeConverter;
    @Autowired
    private ProductCategoryConverter productCategoryConverter;


    @Override
    public PageVO<ProductCategoryTreeNodeVO> findSupplierList(Integer pageNum, Integer pageSize
            , ProductCategoryTreeNodeVO productCategoryTreeNodeVO) {



        //获取级分类
        List<ProductCategory> productCategories = productCategoryList();

        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeSonVOS=productCategoryTreeNodeConverter.convertProductCategoryVOList(productCategories);

        List<ProductCategoryTreeNodeVO> build = CategoryTreeBuilder.build(productCategoryTreeNodeSonVOS);

        PageHelper.startPage(pageNum,pageSize);
        PageInfo<ProductCategoryTreeNodeVO> info =new PageInfo<>(build);
        return new PageVO<ProductCategoryTreeNodeVO>(info.getTotal(),build);
    }

    /** 获取物资类别* */
    @Override
    public List<ProductCategory> productCategoryList() {

        //查找所有级分类
        List<ProductCategory> productCategories = productCategoryMapper.selectAll();
        return productCategories;
    }

    /*添加时查询列表*/

    @Override
    public List<ProductCategoryTreeNodeVO> getParentCategoryTree() {
        List<ProductCategory> productCategories = productCategoryList();
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeSonVOS=productCategoryTreeNodeConverter.convertProductCategoryVOList(productCategories);
        List<ProductCategoryTreeNodeVO> build = CategoryTreeBuilder.build(productCategoryTreeNodeSonVOS);
        return build;


    }

    //添加分类

    @Override
    public Boolean add(ProductCategoryTreeNodeVO productCategoryTreeNodeVO) {
        ProductCategory productCategory = new ProductCategory();
        productCategory.setName(productCategoryTreeNodeVO.getName());
        productCategory.setRemark(productCategoryTreeNodeVO.getRemark());
        productCategory.setSort(productCategoryTreeNodeVO.getSort());
        productCategory.setCreateTime(new Date());
        productCategory.setModifiedTime(new Date());
        productCategory.setPid(productCategoryTreeNodeVO.getPid());

        int insert = productCategoryMapper.insert(productCategory);
        if (insert>0){
            return true;
        }
        return false;
    }


    //删除分类

    @Override
    public Boolean delete(Integer id) {

        Example example = new Example(ProductCategory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("pid",id);

        int isDeleted=0;
        List<ProductCategory> productCategories = productCategoryMapper.selectByExample(example);
        if (productCategories.isEmpty()&&productCategories.size()==0){
            isDeleted= productCategoryMapper.deleteByPrimaryKey(id);
        }

        if (isDeleted>0){
            return true;
        }
        return false;
    }



    //编辑物资分类

    @Override
    public ProductCategoryVO edit(Integer id) {
        ProductCategory productCategory = productCategoryMapper.selectByPrimaryKey(id);
        ProductCategoryVO productCategoryVO = productCategoryConverter.productCategoryVO(productCategory);
        return productCategoryVO;
    }

    //更新

    @Override
    public Boolean update(ProductCategoryVO productCategoryVO, Integer id) {
        ProductCategory productCategory = new ProductCategory();
        productCategory.setId(productCategoryVO.getId());
        productCategory.setPid(productCategoryVO.getPid());
        productCategory.setName(productCategoryVO.getName());
        productCategory.setRemark(productCategoryVO.getRemark());
        productCategory.setSort(productCategoryVO.getSort());
        productCategory.setCreateTime(productCategoryVO.getCreateTime());
        productCategory.setModifiedTime(new Date());

        int i = productCategoryMapper.updateByPrimaryKey(productCategory);
        if (i>0){
            return true;
        }

        return false;
    }

}
